package com.amtf.datafile2.core.excel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;

import com.amtf.datafile2.DataException;
import com.amtf.datafile2.DataField;
import com.amtf.datafile2.cfg.excel.ExcelDataTableDefinition;
import com.amtf.datafile2.core.std.StdDataRecord;

public class ExcelDataRecord extends StdDataRecord<ExcelDataTableDefinition> {

	public ExcelDataRecord(ExcelDataTableDefinition definition) {
		super(definition);
	}

	public void validate() {
		this.clearErrors();
		for (int i = 0; i < fields.length; i++) {
			DataField field = fields[i];
			field.validate();
			if (field.isInvalid()) {
				this.appendError(field.getName() + " " + field.getErrors());
			}
		}
	}

	public String[] getValues() {
		String[] values = new String[fields.length];
		for (int i = 0; i < fields.length; i++) {
			values[i] = fields[i].getValue();
		}
		return values;
	}

	public String toValue() {
		StringBuffer content = new StringBuffer();

		if (fields.length > 0) {
			int max = fields.length - 1;
			for (int i = 0; i < max; i++) {
				if (fields[i] != null) {
					content.append(fields[i].getValue());
				} else {
					content.append("");
				}
				content.append('\t');
			}

			if (fields[max] != null) {
				content.append(fields[max].getValue());
			} else {
				content.append("");
			}
		}

		return content.toString();
	}

	public String toString() {
		StringBuffer content = new StringBuffer();

		if (fields.length > 0) {
			int max = fields.length - 1;
			for (int i = 0; i < max; i++) {
				if (fields[i] != null) {
					content.append(fields[i].toString());
				} else {
					content.append("");
				}
				content.append('\t');
			}

			if (fields[max] != null) {
				content.append(fields[max].toString());
			} else {
				content.append("");
			}
		}

		return content.toString();
	}

	public static ExcelDataRecord parseRecord(ExcelDataTableDefinition definition, int startColumn, int readColumn, Row row, FormulaEvaluator evaluator) throws DataException {
		if (definition.getFieldCount() == 0) {
			int i = startColumn;
			Cell cell = row.getCell(i);
			while (cell != null) {
				definition.addField();
				cell = row.getCell(++i);
			}
		}

		ExcelDataRecord record = definition.newDataRecordInstance();

		for (int i = 0; i < readColumn; i++) {
			Cell cell = row.getCell(i + startColumn);
			if (cell != null) {
				DataField field = record.get(i);

				if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) {
					// TODO
					// field.setValue(toValue(cell, evaluator, field.getDefinition().getParser()));
				} else {

				}
				// field.getDefinition().getFormat()

			} else {
				record.set(i, "");
			}
		}

		return record;
	}

	// public static String toValue(Cell cell, FormulaEvaluator evaluator, DataFieldFormat format) {
	// switch (cell.getCellType()) {
	// case Cell.CELL_TYPE_NUMERIC:
	// if (HSSFDateUtil.isCellDateFormatted(cell)) { // 判断是日期类型
	// Date dt = cell.getDateCellValue();
	// return format.format(dt);
	// } else {
	// return new BigDecimal(cell.getNumericCellValue()).toPlainString();
	// }
	// case Cell.CELL_TYPE_STRING:
	// return cell.getStringCellValue();
	// case Cell.CELL_TYPE_BLANK:
	// return "";
	// case Cell.CELL_TYPE_BOOLEAN:
	// return String.valueOf(cell.getBooleanCellValue());
	// case Cell.CELL_TYPE_FORMULA:
	// CellValue cellValue = evaluator.evaluate(cell);
	// switch (cellValue.getCellType()) {
	// case Cell.CELL_TYPE_NUMERIC:
	// if (HSSFDateUtil.isCellDateFormatted(cell)) { // 判断是日期类型
	// Date dt = cell.getDateCellValue();
	// return format.format(dt);
	// } else {
	// return new BigDecimal(cell.getNumericCellValue()).toPlainString();
	// }
	// case Cell.CELL_TYPE_STRING:
	// return cell.getStringCellValue();
	// case Cell.CELL_TYPE_BOOLEAN:
	// return String.valueOf(cell.getBooleanCellValue());
	// case Cell.CELL_TYPE_BLANK:
	// return "";
	// }
	// }
	//
	// return cell.toString();
	// }

	public static ExcelDataRecord parseRecord(ExcelDataTableDefinition definition, String[] row) throws DataException {
		if (definition.getFieldCount() == 0) {
			definition.addFields(row.length);
		}

		ExcelDataRecord record = definition.newDataRecordInstance();

		record.set(row);
		return record;
	}

	public static ExcelDataRecord parseRecord(ExcelDataTableDefinition definition, Object[] row) throws DataException {
		if (definition.getFieldCount() == 0) {
			definition.addFields(row.length);
		}

		ExcelDataRecord record = definition.newDataRecordInstance();

		record.set(row);
		return record;
	}

}
